
$(function() {
	Tip.emp('申请人');
	
	$("input[id^='date']:visible").datepicker(_date1_option);
});

var CarReqMy = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				
				body.emp = form.emp.value;
				body.urgent = form.urgent.value;
				body.t1 = form.date1.value + " " + form.time1.value;
				body.t2 = form.date2.value + " " + form.time2.value;
				body.name = form.name.value;
				body.address = form.address.value;
								
				body.content = form.content.value;

				$(this).dialog("close");

				var url = 'car.php?do=RequirementMy.addPost';
				
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	view: function(id) {
		var url = 'car.php?do=RequirementMy.view&id=' + id;
		
		Common.dialog_view_(url, '查看信息', '650x400');
	},

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'car.php?do=RequirementMy.edit&id=' + id;
		var c2 = 'car.php?do=RequirementMy.view&id=' + id;
		var c3 = 'car.php?do=RequirementMy.copy&id=' + id;
		
		var cs = 'car.php?do=RequirementMy.status&id=' + id;
		
		var c4 = 'car.php?do=RequirementMy.plan&id=' + id;
		var c5 = 'car.php?do=RequirementMy.reason&id=' + id;
		
		var a1 = ['编辑信息', 'url', 'CarReqMy.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];		
		var a3 = ['复制信息', 'url', 'CarReqMy.dialog_copy_', '650x400'];
		
		var a5 = ['暂停派车', 'url', 'CarReqMy.dialog_stop', '650x400'];
		var a6 = ['取消派车', 'url', 'CarReqMy.dialog_cancel', '650x400'];
		
		// 用车申请
		if (status == 1) {
			var d1 = 'car.php?do=RequirementMy.verify&id=' + id;
			var b1 = ['审核信息', 'url', 'CarReqMy.dialog_verify', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2 + ", " + c3;

			return popup(e, [b1, a1, a2, a3], s);
		}

		// 等待派车
		if (status == 2) {		
			var s = c2 + ", " + c3 + ", " + c5 + ", " + c5;

			return popup(e, [a2, a3, a5, a6], s);
		}

		// 已经派车
		if (status == 3) {		
			var s = c2 + ", " + c3 + ", " + c5 + ", " + c5;

			return popup(e, [a2, a3, a5, a6], s);
		}
		
		// 暂停派车
		if (status == 5) {
			var d1 = 'car.php?do=RequirementMy.status_continue&id=' + id + p2;
			var b1 = ['继续派车', 'url', 'CarReqMy.list_continue'];
			
			var s = d1 + ", " + c2;

			return popup(e, [b1, a2], s);
		}
		
		// 取消派车
		if (status == 6) {
			var d1 = 'car.php?do=RequirementMy.status_restart&id=' + id + p2;
			var b1 = ['重启派车', 'url', 'CarReqMy.list_restart'];
			
			var s = d1 + ", " + c2;

			return popup(e, [b1, a2], s);
		}
			
		var s = c2 + ", " + c3;

		return popup(e, [a2, a3], s);
	},
	
	dialog_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=RequirementMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}				

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 2;
						
						$(this).dialog("close");
						
						var url = 'car.php?do=RequirementMy.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=RequirementMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {			
					"关闭": function() {
						$(this).dialog("close");
					},
			
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
			
						var form = $("#form_edit")[0];
			
						var body = {};
						body.id = form.id.value;
						
						body.emp = form.emp.value;
						body.urgent = form.urgent.value;
						body.t1 = form.date1.value + " " + form.time1.value;
						body.t2 = form.date2.value + " " + form.time2.value;
						body.name = form.name.value;
						body.address = form.address.value;
										
						body.content = form.content.value;
			
						$(this).dialog("close");
			
						var url = 'car.php?do=RequirementMy.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	/**
	 * 复制
	 */
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				
				body.emp = form.emp.value;
				body.urgent = form.urgent.value;
				body.t1 = form.date1.value + " " + form.time1.value;
				body.t2 = form.date2.value + " " + form.time2.value;
				body.name = form.name.value;
				body.address = form.address.value;
								
				body.content = form.content.value;

				$(this).dialog("close");

				var url = 'car.php?do=RequirementMy.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	/**
	 * 暂停
	 */
	dialog_stop: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=RequirementMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否暂停？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 5;

						$(this).dialog("close");

						var url = 'car.php?do=RequirementMy.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 取消
	 */
	dialog_cancel: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=RequirementMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否取消？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 6;

						$(this).dialog("close");

						var url = 'car.php?do=RequirementMy.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	list_restart: function (url, view) {
		$.getJSON('car.php?do=RequirementMy.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否要重启派车？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	list_continue: function (url, view) {
		$.getJSON('car.php?do=RequirementMy.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否要继续派车？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	plan: function (id) {
		var url = "car.php?do=Plan.view&id=" + id;
		$('#dialog_view').dialog('option', 'buttons', { 		
			"关闭": function() { 
				$(this).dialog("close");
			}
		});

		dialog_view(url, '用车安排', '650x400');
	}
};
